<?php
/**
 * 二级管理员
 *
 */
class Admin extends Manager {
	
	function __construct() {
		parent::__construct('manager.id');
	}
		
	/**
	 * 修改管理员
	 */
	public function edit($post) {
		// 当前管理员信息
		$_curManagerinfo = & VS::get('managerinfo');
		
		$id = empty($post['id']) ? 0 : intval($post['id']);
		// 判断是否已经存在
		$select = array('username' => $post['username'],);
		if($id > 0){
			$select['kept'] = " AND `id`!={$id}";
		}
		$rs = $this->db->get_row( $this->tbname, $select, 'id');
		if(!empty($rs)){
			return lang('already').lang('exist');
		}
		$data = array( 
			'username'	=> $post['username'], 
			'role'		=> $post['role'],
			'subset'	=> $_curManagerinfo['userid'],	// 上级管理员的ID
			'memo'		=> $post['memo'], 
		);
		if(!empty($post['password'])){
			$data['password'] = md5(SITE_KEY.$post['password']);
		}
		if($id > 0){
			$data['updatetime'] = TIME;
			$id = $this->db->update( $this->tbname, array('id' => $id, 'subset' => "{$_curManagerinfo['userid']}"), $data);
		}else{
			$data['addtime'] = TIME;
			$id = $this->db->insert( $this->tbname, $data);
		}
		if($id > 0){
			$id = $this->cache_admin($id);
		}
		return $id;
	}
	
	/**
	 * 修改密码
	 *
	 * @param string		$post['password_old']
	 * @param string		$post['password']
	 * @return int
	 */
	public function password($post) {
		$id = empty($post['id']) ? 0 : intval($post['id']);
		if($post['password_old'] == $post['password']) {return '未发生改变';}
		// 判断是否已经存在
		$select = array(
			'id'		=> $id,
			'password'	=> md5(SITE_KEY.$post['password_old']), 
		);
		$rs = $this->db->get_row( $this->tbname, $select, 'id');
		if(empty( $rs)) {return - 1;}
		$data = array( 
			'password' => md5(SITE_KEY.$post['password']), 
			'updatetime' => TIME 
		);
		$r = $this->db->update($this->tbname, array('id' => $id), $data);
		if($r > 0){
			$r = $this->cache_admin($id);
		}
		return $r;
	}
	
	public function setting($post){
		/*******************************************************************************
		 * 系统变量
		 *******************************************************************************/
		$oSysVar = new SysVar();
		$i = 0;
		if(!empty($post['sysvar'])){
			$i ++;
			foreach($post['sysvar'] as $varid => $value){
				$oSysVar->update($varid, array('type' => 3, 'value' => $value));
			}
		}
		if(!empty($post['setting'])){
			if(!empty($post['setting']['search_hot_keyword'])){
				$i ++;
				$_str = str_replace(array("\n", "\r"), ';', $post['setting']['search_hot_keyword']);
				$_str = preg_replace('/;{2,}/', ';', $_str);
				$_str = preg_replace('/\s+;/', ';', $_str);
				$oSysVar->update(8, array('type' => 1, 'value' => $_str));
			}
			if(!empty($post['setting']['email'])){
				$i ++;
				$_arr = array();
				foreach($post['setting']['email'] as $key => $value){
					$_arr[] = "{$key}:{$value}";
				}
				$oSysVar->update(6, array('type' => 1, 'value' => implode(';', $_arr)));
			}
			if(!empty($post['setting']['ucenter'])){
				$i ++;
				$_arr = array();
				foreach($post['setting']['ucenter'] as $key => $value){
					$_arr[] = "{$key}:{$value}";
				}
				$oSysVar->update(7, array('type' => 1, 'value' => implode(';', $_arr)));
			}
		}
		if($i > 0){
			$oSysVar->cache_sysvar();
		}
		/*******************************************************************************
		 * 后台设置
		 *******************************************************************************/
		$id = empty($_POST['id']) ? 0 : trim($_POST['id']);				
		$setting = array(
			'style'		=> $post['style'],
			'language'	=> $post['language'],
			'debug_pr'	=> isset($post['debug_pr']) ? intval($post['debug_pr']) : 0,
			'debug_fb'	=> isset($post['debug_fb']) ? intval($post['debug_fb']) : 0,
		);
		$data = array( 
			'setting'		=> serialize($setting), 
			'memo'			=> $post['memo'],
			'updatetime'	=> time(), 
		);
		$r = $this->db->update( $this->tbname, array('id' => $id), $data);
		if($r > 0){
			$this->refresh($id);
			return $this->cache_admin($id);
		}
		return $r;
	}
	
	/**
	 * 删除管理员
	 *
	 * @param int			$id
	 * @return int
	 */
	public function del($post) {
		// 当前管理员信息
		$_curManagerinfo = & VS::get('managerinfo');
		
		$iRtn = 0;
		if(!is_array( $post['list'])) {
			$post['list'] = array($post['list']);
		}
		foreach($post['list'] as $admin_id){
			$select = array(
				'id'	=> $admin_id,
				'kept'	=> " AND(`username`<>'super' AND `username`<>'admin')",
				'subset'=> "{$_curManagerinfo['userid']}"
			);
			if($_curManagerinfo['userid'] != 1 || $_curManagerinfo['role'] != 1){	// 为了安全
				$select['subset'] = $_curManagerinfo['userid'];
			}
			
			$r = $this->db->delete($this->tbname, $select);
			if($r > 0){
				$iRtn ++;
				$this->cache_del($admin_id);
			}
		}
		return $iRtn;
	}
	
	/**
	 * 取一条记录
	 */
	public function get_row( $id, $subset = null, $fields = '*') {
		$select = array('id' => $id);
		if(isset($subset) && $subset > 0){
			$select['subset'] = $subset;
		}
		return $this->db->get_row( $this->tbname, $select, $fields);
	}
		
	/**
	 * 取列表
	 */
	public function get_list( $fields = '*', $select = array(), $order = array(), $page = 1, $size = 10){
		$sh = array('subset'=> $select['subset']);
		if(!empty($select['role'])){
			$sh['role'] = $select['role'];
		}
		if(!empty($select['keyword'])){
			$sh['kept'] = " AND `username` like '%{$select['keyword']}%'";
		}
		return $this->db->get_list( $this->tbname, $fields, $sh, $order, $page, $size);
	}
	
	// 取全部
	public function get_all( $fields = '*', $select = array(), $order = array()) {
		return $this->db->get_all( $this->tbname, $fields, $select, $order);
	}
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	private function cache_admin($id){
		$info = $this->get_row($id, '*');
		$setting = unserialize($info['setting']);
		return cache_write('manager_'.$id.'.php', array('info' => $info, 'setting' => $setting));
	}
	
	private function cache_del($ids){
		if(!is_array($ids)){
			$ids = array($ids);
		}
		$iRtn = 0;
		foreach($ids as $id){
			if(!cache_delete('manager_'.$id.'.php')){
				return -1;
			}
			$iRtn ++;
		}
		return $iRtn;
	}
}
?>